/**
 * 等待.
 * @param ms 延迟
 * @returns {Promise<unknown>} 无返回
 */
export function sleep(ms) {
    return new Promise(resolve => {
        setTimeout(resolve, ms)
    })
}

/**
 * 节流.
 * @param func 函数
 * @param wait 间隔
 * @returns {(function(...[*]): void)|*} 函数包装
 */
export function throttle(func, wait) {
    let lastTime = 0
    return function (...args) {
        const now = Date.now()
        if (now - lastTime > wait) {
            lastTime = now
            func.apply(this, args)
        }
    }
}

/**
 * 防抖.
 * @param func 函数
 * @param wait 等待
 * @returns {(function(...[*]): void)|*} 包装函数
 */
export function debounce(func, wait) {
    let timeout
    return function (...args) {
        const context = this
        clearTimeout(timeout)
        timeout = setTimeout(() => func.apply(context, args), wait)
    }
}
